package com.jsh.erp.config;

import com.alibaba.ttl.threadpool.TtlExecutors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
@Slf4j
public class ThreadPoolConfig {

    //https://github.com/alibaba/transmittable-thread-local?tab=readme-ov-file#-maven%E4%BE%9D%E8%B5%96
    //TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
// =====================================================
// 在父线程中设置
//context.set("value-set-in-parent");
// =====================================================
    // 在子线程中可以读取，值是"value-set-in-parent"
    //   String value = context.get();


    @Bean(name = "threadPool")
    public ExecutorService threadPoolExecutor() {
        ExecutorService executorService = new ThreadPoolExecutor(6, 6, 600, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100), Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                log.warn("线程池已满");
                if (!executor.isShutdown()) {
                    r.run();
                }
            }
        });
        executorService = TtlExecutors.getTtlExecutorService(executorService);
        return executorService;
    }

}
